//
//  buildInCommand.m
//  serialTest
//
//  Created by mt-007 on 2021/3/23.
//

#import "buildInCommand.h"


@implementation buildInCommand

/**
 * @brief set info.plist file path
 */
- (id)initWithPath {
    if(self = [super init]){
        // get info.plist file path, you must put it in target -> build phases -> Commplie Sources
        filePath = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"plist"];
    }
    return  self;
}


/**
 * @param inputCommand input a string from user input.
 *
 * @return 0 is mean shutdown the program.
 *         1 is mean continue to transimmion.
 *         -1 is mean run build-in command.
 *
 * @brief
 *  else - if input is not a build-in command.
 *  "#done", is mean shutdown the program.
 *  "#author", is mean print the author information.
 *  "#version", is mean print the program version information.
 *
 */
+ (int)command :(NSString *)inputCommand andSystemVar:(char *)systemVar {
    
    
    NSString *commandString = [inputCommand uppercaseStringWithLocale:[NSLocale currentLocale]];
    
    // BASE COMMAND
    NSString *cmdDone = [@"#DONE" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdHelp = [@"#HELP" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdAuthor = [@"#AUTHOR" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdVersion = [@"#VERSION" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdClear = [@"#CLEAR" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdChar = [@"#CHAR" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdBinary = [@"#BIN" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdOctonary = [@"#OCT" uppercaseStringWithLocale:[NSLocale currentLocale]];
    NSString *cmdHexadecimal = [@"#HEX" uppercaseStringWithLocale:[NSLocale currentLocale]];
    
    NSArray *aName = [NSArray arrayWithObjects:cmdDone, cmdHelp, cmdAuthor, cmdVersion, cmdClear, cmdChar, cmdBinary, cmdOctonary, cmdHexadecimal, nil] ;
    
    NSUInteger index = [aName indexOfObject: commandString];
    
    //NSLog(@"%lul", index);
    switch (index) {
        case 0:
            return 0;
        case 1:
            [[[self alloc] initWithPath] printHelp];
            return -1;
        case 2:
            [[[self alloc] initWithPath] printAuthor];
            return -1;
        case 3:
            [[[self alloc] initWithPath] printVersion];
            return -1;
        case 4:
            [[[self alloc] initWithPath] clearConsole];
            return -1;
        case 5:
            //char
            *systemVar = 'c';
            printf("use original character set(ASCII)\n");
            return -1;
        case 6:
            //binary
            *systemVar = 'b';
            printf("use binary character set\n");
            return -1;
        case 7:
            //octonary
            *systemVar = 'o';
            printf("use octonary character set\n");
            return -1;
        case 8:
            //hexadecimal
            *systemVar = 'h';
            printf("use hexadecimal character set\n");
            return -1;
        default:
            break;
    }
    
    // Nothing command match
    return 1;
}

/**
 * Judge the system is Winodws or Linux/Unix, if is Windows run "cls", else run "clear".
 * Because of Xcode is self terminal, so this command will report error:
 * "TERM environment variable not set"
 * But when we use this command in terminal, it will not.
 *
 * Update:
 *  Now, i remove the Windows "cls", beacuse this code only run on MacOS.
 *  So just run clear, and if archive the build package(running on the terminal), the code will not warrning the error.
 *  So $TERM could not suit the terminal.
 */
- (void)clearConsole {
    //system("echo $TERM");
    //system("export TERM=dump");
    system("clear");
}

/**
 * print all build-in command datail.
 */
- (void)printHelp {
    NSLog(@" ----HELP");
    NSLog(@"There is all build-in command: ");
    NSLog(@"#done - Disconnect the serial port and finish the program.");
    NSLog(@"#help - Show all command.");
    NSLog(@"#author - print the author information.");
    NSLog(@"#version - print the version information.");
    NSLog(@"#CHAR - use original character.");
    NSLog(@"#BIN - use binary character.");
    NSLog(@"#OCT - use octonary character.");
    NSLog(@"#HEX - use hexadecimal character.");
}

/**
 * print the author information.
 */
- (void)printAuthor {
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSLog(@"program author is %@", data[@"appAuthor"]);
}

/**
 * Print the version information.
 * Version interview:
 * A.B.C.D ------
 * A: big version(This version will graduate to be 1(NO GUI), or 2(HAVE GUI) )
 * B: small function add or remove.
 * C: fix a bug.
 * D: very very very small change, like add an annotation or add no functional change.(Not fix bug).
 */
- (void)printVersion {
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSLog(@"program version is %@", data[@"appVersion"]);
}

@end
